

const requestMode = require('../../../js/requestsMode.js');
const constants = require('../../../js/constants.js');
const session = require('../../../js/session.js');
const  Tools = require('../../../js/Tools.js');
let addressList = [];
const app = getApp();
let hasDefaultAddress = false;
let defaultAddressId = false;
let Alength = 0;
function initDefaultAdress(data){
    let addressListLength = data.length;
    Alength = addressListLength;
    if(addressListLength == 0){
        hasDefaultAddress =true;
        defaultAddressId = false;
    }
    //else{
    //    defaultAddressId = data[0]['id'];
    //}
}
initDefaultAdress(addressList);
function initDefaultId(data){
    for(let i in data){
        if(data[i]['isDefault'] == true){
            defaultAddressId = data[i]['id'];
        }
    }
}
initDefaultId(addressList);
let modelImgUrl = "../../../images/";
Page({
    data:{
        notSetAddress:modelImgUrl+"notSetAddress.png",
        choseDefaultAddressImg:modelImgUrl+"defaultAddress.png",
        deleteThisImg : modelImgUrl+"deleteAddress.png",
        editThisAddressImg : modelImgUrl + "editAddress.png",
        hasDefaultAddress : hasDefaultAddress,
        addressList:addressList,
        defaultAddressId:defaultAddressId,
        Alength:Alength,
        page:1,
        page_size:parseInt(constants.size),
        page_total : 0
    },
    ///设置默认收货地址
    initDefaultAddress:function(e){
        let id = e.currentTarget.dataset.id;
        let This = this;
        requestMode.sendRequest('address/set-default','POST',{address_id:id},{sKey:session.get('sKey')}).then(function(res){
            let resCode = res.data.code;
            if(resCode == 200){
                This.setData({
                    defaultAddressId : id
                });
            }else if(resCode == -2){
                app.setSkey();
            } else{
                Tools.showErrorToast(res.data.message)
            }
        },function(){
            Tools.showWrongNetWorkToast();
        });
    },

    ///添加地址
    addAddress:function(){
        wx.navigateTo({
            url:'../addAddress/addAddress'
        })
    },
    ///编辑地址
    editAddress:function(e){
        let index = e.currentTarget.dataset.index;
        wx.navigateTo({
            url:'../editAddress/editAddress?index='+index + "&addressArr=" + JSON.stringify(this.data.addressList)
        })
    },

    ///删除地址
    deleteAddress:function(e){
        let id = e.currentTarget.dataset.id;
        let indexOfDel = e.currentTarget.dataset.index;
        let This = this;
        wx.showModal({
            //title: '提示',
            content: '确定删除此地址？',
            success: function(res) {
                if (res.confirm) {
                    requestMode.sendRequest('address/remove','POST',{address_id:id},{sKey:session.get('sKey')}).then(function(res){
                        let resCode = res.data.code;
                        if(resCode == 200){
                            This.setData({
                                deleteId : id
                            });
                            setTimeout(function(){
                                let addressList = This.data.addressList;
                                for(let i in addressList){
                                    if(addressList[i]['id'] == id){
                                        addressList.splice(indexOfDel, 1);
                                    }
                                }
                                initDefaultAdress(addressList);
                                initDefaultId(addressList);
                                This.setData({
                                    addressList:addressList,
                                    Alength:Alength,
                                    defaultAddressId:defaultAddressId,
                                    hasDefaultAddress:hasDefaultAddress
                                })
                            },300)
                        }else{
                            Tools.showErrorToast(res.data.message)
                        }
                    },function(){
                        Tools.showWrongNetWorkToast();
                    });
                }
            }
        });
    },
    getMore:function(page,size,that,type){
        let sKey = session.get('sKey');
        let header = { sKey : sKey };
        Tools.showLoaddingDefault();
        requestMode.sendRequest('address/list','POST',{page:parseInt(page),page_size:parseInt(size)},header).then(function(success){
            Tools.hideLoaddingToast();
            if(success.data.code == 200){
                let getAddressList = (success.data.data.list).reverse();
                if(type == 1){
                    addressList = [];
                    that.setData({ addressList : [] })
                }
                getAddressList.forEach(function(value){
                    if( value['is_default'] == 0 ){
                        value['is_default'] = false;
                    }else{
                        value['is_default'] = true;
                    }
                    addressList.push({
                        address:value['address_full'],
                        name:value['consignee'],
                        phone:value['mobile'],
                        isDefault:value['is_default'],
                        id:value['address_id']
                    })
                });
                initDefaultAdress(addressList);
                initDefaultId(addressList);
                that.setData({
                    page:parseInt(page)+1,
                    hasDefaultAddress : hasDefaultAddress,
                    addressList:addressList,
                    defaultAddressId:defaultAddressId,
                    Alength:Alength
                });
            }else {
                Tools.showErrorToast(success.data.message);
            }
        },function(){
            Tools.showWrongNetWorkToast();
        });
    },
    onLoad:function(res){
        let This = this;
        this.setData({
            page:1
        });
        This.getMore(this.data.page,constants.size,This,1);
    },
    onPullDownRefresh: function () {
        this.onLoad();
    },

    onReachBottom: function () {
        let This = this;
        if (This.data.page >= This.data.page_total) {
            return;
        }
        this.setData({
                page : This.data.page + 1
            }
        );
        This.getMore(This.data.page,This.data.page_size,This,2);
    }
});
